package cn.mihoyo.org.service.impl;

import cn.mihoyo.base.query.BaseQuery;
import cn.mihoyo.base.service.impl.BaseServiceImpl;
import cn.mihoyo.base.util.MD5Utils;
import cn.mihoyo.base.util.PageList;
import cn.mihoyo.base.util.StrUtils;
import cn.mihoyo.org.domain.Employee;
import cn.mihoyo.org.mapper.EmployeeMapper;
import cn.mihoyo.org.service.IEmployeeService;
import cn.mihoyo.org.vo.EmpAndRoleVo;
import cn.mihoyo.system.mapper.RoleMapper;
import cn.mihoyo.user.domain.Logininfo;
import cn.mihoyo.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IEmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;


    @Override
    @Transactional
    public void add(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        String pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(pwd);
        employee.setSalt(salt);
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);
        IntermediateTable(employee);
    }


    private void IntermediateTable(Employee employee) {
        roleMapper.deleteEmpRole(employee.getId());
        roleMapper.addEmpRole(employee.getId(), employee.getRoles());
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setId(employee.getLogininfoId());
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.update(logininfo);
        super.update(employee);
        IntermediateTable(employee);
    }

    @Override
    public void delete(Long id) {
        Employee emp = employeeMapper.findById(id);
        logininfoMapper.delete(emp.getLogininfoId());
        roleMapper.deleteEmpRole(emp.getId());
        super.delete(id);
    }

    @Override
    public List<Employee> findByDeptId(Long deptId) {
        List<Employee> allEmps = this.findAll();
        return allEmps.stream().filter(emp -> deptId.equals(emp.getDepartmentId()))
                .collect(Collectors.toList());
    }


    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> pageList = super.queryByPage(query);
        // 在当前的这个pageList中 将属于他们的roles集合加进去
        List<Employee> emps = pageList.getRows();
        // 获取到emps所有的 id集合
        List<Long> empId = emps.stream().map(Employee::getId).collect(Collectors.toList());
        // 根据empIds 查询对应的所有的 roleIds 只需要查询中间表即可
        List<EmpAndRoleVo> resultTmp = roleMapper.findByEmpIds(empId);
        // 将集合根据 empid进行分组
        Map<Long, List<Long>> roleMap = resultTmp.stream().collect(Collectors.groupingBy(EmpAndRoleVo::getEmployeeId
                , Collectors.mapping(EmpAndRoleVo::getRoleId, Collectors.toList())));
        emps.forEach(emp -> {
            List<Long> roleIds = roleMap.get(emp.getId());
            emp.setRoles(roleIds);
        });

        return pageList;
    }
}
